﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using TTGame;

/*
 * AnimEvents
 * 用于技能编辑器上的各种事件
 *
 *
 * 依赖：
 *
 * ！不写重复代码！
 * @author TTun
 * @date 7/24/2017 10:24:37 AM
 */
namespace TTGameEngine {
public class ParticleControl : MonoBehaviour {

    public string particleName;   //粒子Name
    public float lastTime; //粒子持续时间

    Transform relationBone; //关联骨骼
    GameObject m_particleNode;

    public void Init(Transform bindTF) {
        relationBone = bindTF;
    }

    public void Deactivate() {
        Destroy(m_particleNode);
        Destroy(gameObject);
        m_particleNode = null;
    }

    public void OutBreak(Puppet puppet) {
        //粒子自身带脚本自动消失
        GameObject prefab = GameObjectLoader.Ins.GetPrefabObj(GameConstant.PrefabPath_Effect + particleName) as GameObject;
        if(prefab == null) {
            return;
        }
        m_particleNode = Instantiate(prefab) as GameObject;
        if(m_particleNode == null) {
            DebugUtil.LogError("[ParticleControl] name =" + particleName + "not existed!!");
            return;
        }
        if(relationBone) {
            Quaternion rotation = m_particleNode.transform.localRotation;
            m_particleNode.transform.parent = relationBone;
            m_particleNode.transform.localRotation = rotation;
            m_particleNode.transform.localPosition = Vector3.zero;
        } else {
            if(puppet) {
                //m_particleNode.transform.localRotation = relationBone.localRotation;
                m_particleNode.transform.position = puppet.objTF.position;
            }
        }
        if(lastTime > 0f) {
            Destroy(m_particleNode, lastTime);
            Destroy(gameObject, lastTime);
        }
    }
}
}